﻿using System;
using System.IO;
using MetaMapics.Pbf;

namespace Sample_Program
{
  class Program
  {
    private class SampleClass: IPbfStreamable
    {
      public string Name { get; set; }
      public int Age { get; set; }

      public void WriteToPbf(PbfWriter writer)
      {
        writer.Write(1, Name);
        writer.Write(2, Age);
      }

      public void ReadFromPbf(PbfReader reader)
      {
        foreach (uint fieldNum in reader.Fields())
        {
          switch (fieldNum)
          {
            case 1:
              Name = reader.ReadString();
              break;
            case 2:
              Age = reader.ReadInt();
              break;
            default:
              reader.IgnoreField();
              break;
          }
        }
      }
    }

    static void Main(string[] args)
    {
      var data = new SampleClass { Name = "Fred", Age = 21 };
      using (Stream stream = File.OpenWrite("data.pbf"))
      using (var writer = new PbfWriter(stream))
      {
        Console.WriteLine("Writing data to PBF file");
        Console.WriteLine();
        data.WriteToPbf(writer);
      }

      var data2 = new SampleClass();
      using (Stream stream = File.OpenRead("data.pbf"))
      using (var reader = new PbfReader(stream))
      {
        Console.WriteLine("Reading data from PBF file");
        data2.ReadFromPbf(reader);
        Console.WriteLine("Name: " + data2.Name);
        Console.WriteLine("Age: " + data2.Age.ToString());
      }
      Console.WriteLine();
      Console.WriteLine("Press any key");
      Console.ReadKey();
    }
  }
}
